package com.juyu.miliao.api.controller.whitepacket;

import com.juyu.miliao.api.constant.NightChatInTheSameCityRedisKey;
import com.juyu.miliao.api.domain.VideoComment;
import com.juyu.miliao.api.domain.vo.NightChatInTheSameCityVideoCommentVo;
import com.juyu.miliao.api.domain.vo.NightChatInTheSameCityVo;
import com.juyu.miliao.api.domain.vo.VideoCommentVO;
import com.juyu.miliao.api.service.UserVideoService;
import com.juyu.miliao.api.service.VideoCommentService;
import com.juyu.miliao.api.service.whitepacket.NightChatInTheSameCityService;
import com.juyu.miliao.api.validator.common.Save;
import com.juyu.miliao.common.domain.BaseQuery;
import com.juyu.miliao.common.domain.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 同城夜聊
 *
 * @author xuanqicheng
 * @create 2019-07-15:09
 */
@RestController
@RequestMapping("/nightChat")
public class NightChatInTheSameCityContoller {

    @Autowired
    private NightChatInTheSameCityService nightChatInTheSameCityService;

    @Autowired
    private UserVideoService userVideoService;

    @Autowired
    private VideoCommentService videoCommentService;

    /**
     * 获取发现列表
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/list/{page}-{pageSize}")
    public Object getList(@PathVariable("page") Integer page, @PathVariable("pageSize") Integer pageSize){
        if (page <= 0) {
            page = 1;
        }
        if (pageSize <= 0) {
            pageSize = 20;
        }
        return nightChatInTheSameCityService.getList(page,pageSize);
    }

    /**
     * 根据用户id获取发现列表
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/list/{userId}/{page}-{pageSize}")
    public Object getListByUserId(@PathVariable("userId") long userId,
                                  @PathVariable("page") Integer page,
                                  @PathVariable("pageSize") Integer pageSize){
        if (page <= 0) {
            page = 1;
        }
        if (pageSize <= 0) {
            pageSize = 20;
        }
        return nightChatInTheSameCityService.getListByUserId(userId,page,pageSize);
    }

    /**
     * 添加操作
     * @param nightChatInTheSameCityVo
     */
    @PostMapping("/save")
    public void addFindInfo(Authentication token, NightChatInTheSameCityVo nightChatInTheSameCityVo){

        Subject subject = (Subject) token.getPrincipal();

        nightChatInTheSameCityVo.setUserId(subject.getId());

        nightChatInTheSameCityService.addInfo(nightChatInTheSameCityVo);
    }

    /**
     * 点赞完成
     * @param token
     * @param videoId
     * @return
     */
    @GetMapping("/love/{videoId}")
    public Object addUserVideoLove(Authentication token, @PathVariable("videoId") String videoId) {
        Subject subject = (Subject) token.getPrincipal();
        Long userId = subject.getId();
        if(videoId.startsWith(NightChatInTheSameCityRedisKey.RAW_DATA_TAG)){
            long vId = Long.parseLong(videoId.replace(NightChatInTheSameCityRedisKey.RAW_DATA_TAG,""));
            return userVideoService.clickVideoLove(userId, vId);
        }else if(videoId.startsWith(NightChatInTheSameCityRedisKey.NEW_DATA_TAG)){
            return nightChatInTheSameCityService.clickVideoLove(userId,videoId);
        }
        return null;
    }

    /**
     * 添加评论
     * @param token
     * @param videoComment
     */
    @PostMapping("/comment")
    public void save(Authentication token,
                     @Validated(value = Save.class) NightChatInTheSameCityVideoCommentVo videoComment) {
        Subject subject = (Subject) token.getPrincipal();
        Long userId = subject.getId();
        if(videoComment.getVideoId().startsWith(NightChatInTheSameCityRedisKey.RAW_DATA_TAG)){
            VideoComment obj = new VideoComment();
            obj.setVideoId(Long.parseLong(videoComment.getVideoId()
                    .replace(NightChatInTheSameCityRedisKey.RAW_DATA_TAG,"")));
            obj.setContent(videoComment.getContent());
            videoCommentService.save(userId, obj);
        }else if(videoComment.getVideoId().startsWith(NightChatInTheSameCityRedisKey.NEW_DATA_TAG)){

            nightChatInTheSameCityService.saveVideoComment(userId,videoComment);
        }

    }


    /**
     * 获取评论列表
     *
     * @param videoId   videoId
     * @param baseQuery baseQuery
     * @return List<VideoComment>
     */
    @GetMapping(value = "/comment/{videoId}/{pageNum}-{pageSize}")
    public Object list(BaseQuery baseQuery,
                               @PathVariable("videoId") String videoId,
                               @PathVariable("pageNum") String pageNum,
                               @PathVariable("pageSize") String pageSize) {
        if(videoId.startsWith(NightChatInTheSameCityRedisKey.RAW_DATA_TAG)){
            long vId = Long.parseLong(videoId.replace(NightChatInTheSameCityRedisKey.RAW_DATA_TAG,""));
            VideoCommentVO videoCommentVO = videoCommentService.list(vId, baseQuery);
            Map<String,Object> map = new HashMap<>(2);
            map.put("rows",videoCommentVO.getRows());
            List<NightChatInTheSameCityVideoCommentVo> commentsList = new ArrayList<>();
            List<VideoComment> list = videoCommentVO.getComments();
            for(VideoComment videoComment: list){
                NightChatInTheSameCityVideoCommentVo vo =
                        new NightChatInTheSameCityVideoCommentVo();

                BeanUtils.copyProperties(videoComment,vo);
                vo.setId(
                        NightChatInTheSameCityRedisKey.VIDEO_COMMENT_RAW_DATA_TAG+videoComment.getId())
                .setVideoId(NightChatInTheSameCityRedisKey.RAW_DATA_TAG+videoComment.getVideoId())
                .setCreateTime(videoComment.getCreateTime().toLocalDateTime());

                commentsList.add(vo);
            }
            map.put("comments",commentsList);
            return map;
        }else{
            return nightChatInTheSameCityService.getVideoCommentList(videoId,baseQuery);
        }

    }



}
